package com.net.oil.car.CardViewPager;

import android.content.Context;

import androidx.cardview.widget.CardView;
import androidx.viewpager.widget.PagerAdapter;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;


import com.net.oil.R;
import com.net.oil.car.bean.CarBean;
import com.net.oil.http.AppUrl;
import com.net.oil.util.StringUtil;
import com.net.oil.util.glide.GlideUtil;

import java.util.ArrayList;
import java.util.List;



public class CardPagerAdapter extends PagerAdapter implements CardAdapter {

    private List<CardView> mViews;
    private List<CarBean> mData;
    private Context context;

    private int MaxElevationFactor = 9;

    @Override
    public int getMaxElevationFactor() {
        return MaxElevationFactor;
    }

    @Override
    public void setMaxElevationFactor(int MaxElevationFactor) {
        this.MaxElevationFactor = MaxElevationFactor;
    }

    public CardPagerAdapter(Context context, int width) {
        mData = new ArrayList<>();
        mViews = new ArrayList<>();
        this.context = context;
        int maxFactor = width / 16;
        setMaxElevationFactor(maxFactor);
    }

    @Override
    public int getItemPosition(Object object) {
        return POSITION_NONE;
    }

    public void updateData(List<CarBean> mData){
        this.mData.clear();
        mViews.clear();
        this.mData.addAll(mData);
        notifyDataSetChanged();
    }


    @Override
    public CardView getCardViewAt(int position) {
        return mViews.get(position);
    }

    @Override
    public int getCount() {
        return mData.size();
    }

    @Override
    public boolean isViewFromObject(View view, Object object) {
        return view == object;
    }

    @Override
    public Object instantiateItem(ViewGroup container, final int position) {

        CarBean carBean = mData.get(position);

        View view = LayoutInflater.from(context).inflate(R.layout.card_view_item, container, false);
        view.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (cardItemClickListener != null) {
                    cardItemClickListener.onClick(position);
                }
            }
        });
        container.addView(view);
        CardView cardView = view.findViewById(R.id.cardView);
        ImageView carLogo = view.findViewById(R.id.car_logo);
        ImageView defaultCarIv = view.findViewById(R.id.default_car_iv);
        TextView carName = view.findViewById(R.id.car_name);
        TextView carType = view.findViewById(R.id.car_type);

        if (carBean != null){
            CarBean.CarModelInfoBean carInfo = carBean.getCarModelInfo();
            if (carBean.isIsDefault()){
                defaultCarIv.setVisibility(View.VISIBLE);
            }else {
                defaultCarIv.setVisibility(View.GONE);
            }
            if (carInfo != null){
                if (!StringUtil.isEmpty(carInfo.getBrand())){
                    carName.setText(carInfo.getBrand());
                }

                if (!StringUtil.isEmpty(carInfo.getType())){
                    carType.setText(carInfo.getType());
                }

                if (!StringUtil.isEmpty(carInfo.getBrandLogo())){
                    // TODO: 2018/4/11 0011 错误图片
                    GlideUtil.getInstance().loadImage(context, carLogo, AppUrl.getImageUrl(carInfo.getBrandLogo()), R.drawable.car_logo_err);
                }
            }
        }

        cardView.setMaxCardElevation(MaxElevationFactor);
        if (position <= mViews.size()){
            mViews.add(position, cardView);
        }else {
            mViews.add(cardView);
        }

        return view;
    }

    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
        container.removeView((View) object);
        if (mViews.size() > 0){
            mViews.clear();
        }
    }


    private OnCardItemClickListener cardItemClickListener;

    public interface OnCardItemClickListener {
        void onClick(int position);
    }

    public void setOnCardItemClickListener(OnCardItemClickListener cardItemClickListener) {
        this.cardItemClickListener = cardItemClickListener;
    }

}
